package com.czwj.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.czwj.dao.EquipmentMapper;
import com.czwj.entities.Equipment;
import com.czwj.service.EquipmentService;
import com.czwj.util.TextUtil;

/**
* @ClassName: EquipmentServiceImpl
* @Description: TODO
* @author H
* @date 2020年4月9日
*
*/
@Service
public class EquipmentServiceImpl implements EquipmentService{
	@Resource
	EquipmentMapper mapper;
	/** 
	* 
	* @param map
	* @return
	* @see com.czwj.service.EquipmentService#search(java.util.Map)
	*/
	@Override
	public List<Equipment> search(Map<String,Object> map){
		// TODO Auto-generated method stub
		return this.mapper.search(map);
	}
	
	/** 
	* 
	* @param map
	* @return
	* @see com.czwj.service.EquipmentService#count(java.util.Map)
	*/
	@Override
	public int count(Map<String,Object> map){
		// TODO Auto-generated method stub
		return this.mapper.count(map);
	}
	
	/** 
	* 
	* @param equipment
	* @return
	* @see com.czwj.service.EquipmentService#update(com.czwj.entities.Equipment)
	*/
	@Override
	@Transactional
	public int update(Equipment equipment){
		// TODO Auto-generated method stub
		if(TextUtil.isNotEmpty(equipment.getCode())) {
			Map<String, Object> map = new HashMap<>();
			map.put("code", equipment.getCode());
			List<Equipment> list = this.mapper.search(map);
			for (Equipment equipment2 : list) {
				equipment2.setCode("");
				this.mapper.updateByPrimaryKeySelective(equipment2);
			}
		}
		
		return this.mapper.updateByPrimaryKeySelective(equipment);
	}
	
	/** 
	* 
	* @param equipment
	* @return
	* @see com.czwj.service.EquipmentService#insert(com.czwj.entities.Equipment)
	*/
	@Override
	public int insert(Equipment equipment){
		// TODO Auto-generated method stub
		if(TextUtil.isNotEmpty(equipment.getCode())) {
			Map<String, Object> map = new HashMap<>();
			map.put("code", equipment.getCode());
			List<Equipment> list = this.mapper.search(map);
			for (Equipment equipment2 : list) {
				equipment2.setCode("");
				this.mapper.updateByPrimaryKeySelective(equipment2);
			}
		}
		return this.mapper.insertSelective(equipment);
	}
	
	/** 
	* 
	* @param id
	* @return
	* @see com.czwj.service.EquipmentService#delete(int)
	*/
	@Override
	public int delete(int id){
		// TODO Auto-generated method stub
		return this.mapper.deleteByPrimaryKey(id);
	}

	/** 
	* 
	* @param id
	* @return
	* @see com.czwj.service.EquipmentService#selectByPrimaryKey(java.lang.Integer)
	*/
	@Override
	public Equipment selectByPrimaryKey(Integer id){
		// TODO Auto-generated method stub
		return this.mapper.selectByPrimaryKey(id);
	}

	@Override
	public List<Equipment> searchWithPressure(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return this.mapper.searchWithPressure(map);
	}

	@Override
	public Equipment selectWithPressureByPrimaryKey(Integer id) {
		// TODO Auto-generated method stub
		return this.mapper.selectWithPressureByPrimaryKey(id);
	}

	@Override
	public Equipment findByCode(String imei) {
		// TODO Auto-generated method stub
		return this.mapper.findByCode(imei);
	}
}
